package hero.mps.web.sys_role.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hero.mps.web.sys_menu.entity.SysMenu;
import hero.mps.web.sys_menu.service.SysMenuService;
import hero.mps.web.sys_menu.utils.MakeTree;
import hero.mps.web.sys_role.entity.AssignParam;
import hero.mps.web.sys_role.entity.AssignVO;
import hero.mps.web.sys_role.entity.RoleParam;
import hero.mps.web.sys_role.entity.SysRole;
import hero.mps.web.sys_role.mapper.SysRoleMapper;
import hero.mps.web.sys_role.service.SysRoleService;
import hero.mps.web.system_user.entity.SysUser;
import hero.mps.web.system_user.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
    private final SysUserService sysUserService;
    private final SysMenuService sysMenuService;

    @Override
    public IPage<SysRole> list(RoleParam roleParam) {
        // 构造分页对象
        IPage<SysRole> page = new Page<>();
        page.setSize(roleParam.getPageSize());
        page.setCurrent(roleParam.getCurrentPage());
        // 构建查询条件
        QueryWrapper<SysRole> query = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(roleParam.getRoleName())){
            query.lambda().like(SysRole::getRoleName, roleParam.getRoleName());
        }
        return this.baseMapper.selectPage(page, query);
    }

    @Override
    public AssignVO getAssignVOShow(AssignParam assignParam) {
        // 根据用户id查询用户
        SysUser user = sysUserService.getById(assignParam.getUserId());
        List<SysMenu> list = null;
        // 判断用户是否是超级管理员
        if (user.getIsAdmin().equals(1)) {
            // 如果是1，则证明是超级管理员，拥有所有权限
            QueryWrapper<SysMenu> query = new QueryWrapper<>();
            query.lambda().orderByAsc(SysMenu::getOrderNum);
            // 查询菜单
            list = sysMenuService.list(query);
        } else {
            // 不是超级管理员，根据用户id查询
            list = sysMenuService.getMenuByUserId(assignParam.getUserId());
        }
        // 组装菜单树
        List<SysMenu> sysMenus = MakeTree.makeMenuTree(list, 0);
        // 查询角色以来的菜单
        List<SysMenu> roleList = sysMenuService.getMenuByRoleId(assignParam.getRoleId());
        // 过滤出菜单id
        List<Integer> ids = new ArrayList<>();
        Optional.ofNullable(roleList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null )
                .forEach(item -> {
                    ids.add(item.getMenuId());
                });
        // 组装返回值
        AssignVO assignVO = new AssignVO();
        assignVO.setSysMenuList(sysMenus);
        assignVO.setCheckList(ids.toArray());
        return assignVO;
    }
}
